package com.xrj.generate;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Arrays;

/**
 * 此代码生成器是3.5.1版本以下的mybatis-plus-generator 使用
 * 使用此生成器先导入依赖,mybatis_plus,mybatis_generate,模板引擎,swagger,对应数据库驱动等
 *
 * @author xrj
 * @since 2021/12/30 17:33
 */
public class MysqlAutoGenerator {
    private static final String OUT_DIR = System.getProperty("user.dir") + "/generate/src/main/java"; //项目根路径

    private static final String PROJECT_DIR = "com.xrj.generate"; //项目包路径

    private static final String URL = "jdbc:mysql://localhost:3306/csmall";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "123456";
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    private static final DbType NOW_DB = DbType.MYSQL;

    /**
     * 需要生成的表名称
     */
    private static final String[] GENERATE_TABLES = {"tb_address","tb_express","tb_order","tb_order_item","tb_order_shipping","tb_payment","tb_promo_item","tb_promo_session","tb_refund","tb_stock","tb_user_verify"};


    public static void main(String[] args) {
        System.out.println("文件生成路径根路径:" + OUT_DIR + "/" + PROJECT_DIR);
        System.out.println("数据库地址:" + URL);
        System.out.println("表名:" + Arrays.toString(GENERATE_TABLES));

        AutoGenerator generator = new AutoGenerator();

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(NOW_DB);
        dsc.setUrl(URL);
        dsc.setUsername(USERNAME);
        dsc.setPassword(PASSWORD);
        dsc.setDriverName(DRIVER_NAME);
        generator.setDataSource(dsc);

        // 全局配置
        GlobalConfig gbc = new GlobalConfig();
        gbc.setOutputDir(OUT_DIR)// 项目java目录绝对路径,默认:"D://"
                .setFileOverride(true)// 是否覆盖,默认false
                .setActiveRecord(true) //ActiveRecord模式,默认false,对象直接调用insert()插入数据,如:user.insert()
                .setEnableCache(false)// 是否开启二级缓存,默认false
                .setBaseResultMap(true)// 是否生成的xml文件中ResultMap映射,默认false
                .setBaseColumnList(true)// 是否生成的xml文件中字段列表,默认false
                .setAuthor("xrj") //作者,生成注释
                .setSwagger2(false)
                // 自定义文件命名,注意 %s 会自动填充表实体属性！
                .setMapperName("%sMapper")
                .setXmlName("%sMapper")
                .setServiceName("%sService")
                .setServiceImplName("%sServiceImpl")
                .setControllerName("%sController");
        generator.setGlobalConfig(gbc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setCapitalMode(false) //大小写敏感,默认false
                .setEntityBuilderModel(true) //实体类是否建造者模型,默认false
                .setEntityLombokModel(true) //实体类是否使用lombok,默认false
                .setRestControllerStyle(true) //是否生成controller控制器,默认false
                .setEntityTableFieldAnnotationEnable(true) //是否生成实体时,生成字段注解,默认false
                .setNaming(NamingStrategy.underline_to_camel) // 表名生成策略,默认no_change
                .setInclude(GENERATE_TABLES); // 需要生成的表
        generator.setStrategy(strategy);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent(PROJECT_DIR) //根目录下续,起始目录,默认:"com.baomidou"
                .setModuleName(null) //起始目录下中间目录,默认:null
                .setEntity("entity") //以下为生成路径包名,均是默认值
                .setMapper("mapper")
                .setXml("mapper.xml")
                .setService("service")
                .setServiceImpl("service.impl")
                .setController("controller");
        generator.setPackageInfo(pc);

        //设置模板引擎,需要导入对应模板引擎的依赖,使用Freemarker引擎模板，默认的是Velocity引擎模板
        generator.setTemplateEngine(new FreemarkerTemplateEngine());

        generator.execute();
    }
}
